package it.univpm.diiga.memorysim.views;
 
import it.univpm.diiga.memorysim.data.DataPlugin;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Group;

public class SelectSimulation extends ViewPart {

	public static final String ID = "it.univpm.diiga.memorysim.views.SelectSimulation"; 
	
	private static String[] Gmem1=new String[]{"Paginazione","Segmentazione"};
	private static String[] Gmem2=DataPlugin.kernel.getAlgorithmSecMem();
	private static String[] metodiSost=DataPlugin.kernel.getAlgorithmCentMem();
	
	//private String[] Gmem2=DataPlugin.getAlgorithsCenMem();
	//private String[] metodiSost=DataPlugin.getAlgorithsSecMem();
	
	private static Composite top = null;
	private Label selectSimulationLabel = null;
	private Button button1 = null;
	private Button button2 = null;
	private Button button3 = null;
	private Group group = null;
	private Label label1 = null;
	private static Combo AllocMem = null;
	private Group group2 = null;
	private Label label12 = null;
	private static Combo combo2 = null;
	private Label label11 = null;

	private Button button;
	private static Combo combo11 = null;

	//private Button button = null;


	public void createPartControl(Composite arg0) {
		GridData gridData3 = new GridData();
		gridData3.horizontalAlignment = GridData.FILL;
		gridData3.verticalAlignment = GridData.CENTER;
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.FILL;
		gridData2.verticalAlignment = GridData.CENTER;
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.CENTER;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 1;
		gridLayout.verticalSpacing = 5;
		gridLayout.marginWidth = 5;
		gridLayout.horizontalSpacing = 5;
		GridData gridData1 = new GridData();
		gridData1.horizontalAlignment = GridData.FILL;
		gridData1.grabExcessVerticalSpace = false;
		gridData1.grabExcessHorizontalSpace = false;
		gridData1.verticalAlignment = GridData.CENTER;
		top = new Composite(arg0, SWT.NONE);
		selectSimulationLabel = new Label(top, SWT.NONE);
		selectSimulationLabel.setText("Quale simulazione si intende avviare?");
		selectSimulationLabel.setLayoutData(gridData1);
		button1 = new Button(top, SWT.NONE);
		button1.setText("Simulazione MEMORIA CENTRALE");
		button1.setLayoutData(gridData3);
		
		button2 = new Button(top, SWT.NONE);
		button2.setText("Simulazione MEMORIA SECONDARIA");
		button2.setLayoutData(gridData2);
		button3 = new Button(top, SWT.NONE);
		button3.setText("Simulazione IBRIDA");
		button3.setLayoutData(gridData);
		createGroup();
		createGroup2();
		top.setLayout(gridLayout);
		button = new Button(top, SWT.NONE);
		button.setText("Procedi");
		button1.addSelectionListener(new SelectionListener() {

			
			public void widgetDefaultSelected(SelectionEvent e) {
				
				
			}

			
			public void widgetSelected(SelectionEvent e) {
				DataPlugin.SimType=1;
				AllocMem.setEnabled(true);
				combo11.setEnabled(true);
				combo2.setEnabled(false);
			}
          });
		button2.addSelectionListener(new SelectionListener() {

			
			public void widgetDefaultSelected(SelectionEvent e) {
				
				
			}

			
			public void widgetSelected(SelectionEvent e) {
				DataPlugin.SimType=2;
				AllocMem.setEnabled(false);
				combo11.setEnabled(false);
				combo2.setEnabled(true);
			}
          });
		button3.addSelectionListener(new SelectionListener() {

			
			public void widgetDefaultSelected(SelectionEvent e) {
				
				
			}

			
			public void widgetSelected(SelectionEvent e) {
				DataPlugin.SimType=3;
				AllocMem.setEnabled(true);
				combo11.setEnabled(true);
				combo2.setEnabled(true);
			}
          });
		button.addSelectionListener(new SelectionListener() {

			
			public void widgetDefaultSelected(SelectionEvent e) {
				
				
			}

			
			public void widgetSelected(SelectionEvent e) {
				try{
					int type=DataPlugin.SimType;
					if(type==1){
						getSelection1();
					}
					if(type==2){
						getSelection2();
					}
					if(type==3){
						getSelection1();
						getSelection2();
					}
					
				IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
				//window.getActivePage().resetPerspective();
				//window.getActivePage().findView(Paginazione.ID).dispose();
				
				try {
					 window.getActivePage().showView(Param.ID, "Parametri", IWorkbenchPage.VIEW_ACTIVATE);
					 //window.getActivePage().findView(Paginazione.ID).dispose();
				} catch (PartInitException e1) {
					MessageDialog.openError(null, "Error", "Error opening view:" + e1.getMessage());
				}
				}catch(Exception ex){
				   MessageDialog.openInformation(null, "Tipo non valido", "Bisogna selezionare tutte le caratteristiche della simulazione");
				}

			}
          });
		addLoader();
		//addListeners();
	}

	public static void addLoader() {
		resetSelection();
			for(int i=0;i<Gmem1.length;i++){
				if(Gmem1[i].equals(DataPlugin.SimSelected)) AllocMem.select(i);
			}
			for(int j=0;j<Gmem2.length;j++){
				if(Gmem2[j].equals(DataPlugin.SimSelected1)) combo2.select(j);
			}
		
			for(int k=0;k<metodiSost.length;k++){
				if(metodiSost[k].equals(DataPlugin.AlgSelected)) combo11.select(k);
			}

		if(DataPlugin.SimType==0){
			AllocMem.setEnabled(false);
			combo11.setEnabled(false);
			combo2.setEnabled(false);
		}
		if(DataPlugin.SimType==1){
				AllocMem.setEnabled(true);
				combo11.setEnabled(true);
				combo2.setEnabled(false);
		}
		if(DataPlugin.SimType==2){			
			AllocMem.setEnabled(false);
			combo11.setEnabled(false);
			combo2.setEnabled(true);
		}
		if(DataPlugin.SimType==3){			
			AllocMem.setEnabled(true);
			combo11.setEnabled(true);
			combo2.setEnabled(true);
		}
	}
/*
	private void addListeners() {
		ModifyListener listener = new ModifyListener(){
			public void modifyText(ModifyEvent e) {
				try{
					int type=DataPlugin.SimType;
					if(type==1){
						getSelection1();
					}
					if(type==2){
						getSelection2();
					}
					if(type==3){
						getSelection1();
						getSelection2();
					}
					
				IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
				//window.getActivePage().resetPerspective();
				//window.getActivePage().findView(Paginazione.ID).dispose();
				
				try {
					 window.getActivePage().showView(Param.ID, "Parametri", IWorkbenchPage.VIEW_ACTIVATE);
					 //window.getActivePage().findView(Paginazione.ID).dispose();
				} catch (PartInitException e1) {
					MessageDialog.openError(null, "Error", "Error opening view:" + e1.getMessage());
				}
				}catch(Exception ex){
				   //MessageDialog.openInformation(null, "Tipo non valido", "Bisogna selezionare tutte le caratteristiche della simulazione");
				}
	
			}
			
		};
		AllocMem.addModifyListener(listener);
		combo2.addModifyListener(listener);
		combo11.addModifyListener(listener);
		
	}
*/
	
	public void setFocus() {
		

	}

	/**
	 * This method initializes group	
	 *
	 */
	private void createGroup() {
		GridData gridData8 = new GridData();
		gridData8.horizontalAlignment = GridData.FILL;
		gridData8.grabExcessHorizontalSpace = true;
		gridData8.verticalAlignment = GridData.CENTER;
		GridData gridData6 = new GridData();
		gridData6.horizontalAlignment = GridData.FILL;
		gridData6.verticalAlignment = GridData.CENTER;
		group = new Group(top, SWT.NONE);
		group.setLayout(new GridLayout());
		group.setLayoutData(gridData6);
		group.setText("Simulazione MEMORIA CENTRALE");
		label1 = new Label(group, SWT.NONE);
		label1.setText("Metodo di gestione");
		label1.setLayoutData(gridData8);
		createCombo();
		label11 = new Label(group, SWT.NONE);
		label11.setText("Metodo di sostituzione");
		createCombo11();
	}

	/**
	 * This method initializes AllocMem	
	 *
	 */
	private void createCombo() {
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = GridData.FILL;
		gridData4.verticalAlignment = GridData.CENTER;
		AllocMem = new Combo(group, SWT.NONE);
		AllocMem.setLayoutData(gridData4);
		for(String element:Gmem1) AllocMem.add(element);
		AllocMem.setEnabled(false);
		AllocMem.setText("");
	}

	/**
	 * This method initializes group2	
	 *
	 */
	private void createGroup2() {
		GridData gridData9 = new GridData();
		gridData9.horizontalAlignment = GridData.FILL;
		gridData9.verticalAlignment = GridData.CENTER;
		GridData gridData7 = new GridData();
		gridData7.horizontalAlignment = GridData.FILL;
		gridData7.verticalAlignment = GridData.CENTER;
		group2 = new Group(top, SWT.NONE);
		group2.setLayout(new GridLayout());
		group2.setLayoutData(gridData7);
		group2.setText("Simulazione MEMORIA SECONDARIA");
		label12 = new Label(group2, SWT.NONE);
		label12.setText("Metodo di allocazione");
		label12.setLayoutData(gridData9);
		createCombo2();
	}

	/**
	 * This method initializes combo2	
	 *
	 */
	private void createCombo2() {
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = GridData.FILL;
		gridData5.grabExcessHorizontalSpace = true;
		gridData5.verticalAlignment = GridData.CENTER;
		combo2 = new Combo(group2, SWT.NONE);
		combo2.setLayoutData(gridData5);
		for(String element:Gmem2) combo2.add(element);
		combo2.setEnabled(false);
		combo2.setText("");
	}

	/**
	 * This method initializes combo11	
	 *
	 */
	private void createCombo11() {
		GridData gridData10 = new GridData();
		gridData10.horizontalAlignment = GridData.FILL;
		gridData10.verticalAlignment = GridData.CENTER;
		combo11 = new Combo(group, SWT.NONE);
		combo11.setLayoutData(gridData10);
		for(String element:metodiSost) combo11.add(element);
		combo11.setEnabled(false);
		combo11.select(-1);
	}
	
	private void getSelection1() {
		int i= AllocMem.getSelectionIndex();
		DataPlugin.SimSelected=Gmem1[i];
		int j= combo11.getSelectionIndex();
		DataPlugin.AlgSelected=metodiSost[j];
	}
	private void getSelection2() {
		int i= combo2.getSelectionIndex();
		DataPlugin.SimSelected1=Gmem2[i];
	}

	public static Composite getTop(){
		return top;
	}

	public static void resetSelection(){
		AllocMem.setText("");
		combo11.setText("");
		combo2.setText("");
		AllocMem.setEnabled(false);
		combo11.setEnabled(false);
		combo2.setEnabled(false);
	}
}
